/**
本文件内的主要是流量相关接口
Auth： hzp
Date：2020-02-26
*/
package v1

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"zimuzu_cvn_web_api/config"
	"zimuzu_cvn_web_api/pkg/e"
	"zimuzu_cvn_web_api/pkg/errno"
	"zimuzu_cvn_web_api/router/api"
	"zimuzu_cvn_web_api/router/api/app/middleware"
	"zimuzu_cvn_web_api/service/flows"
)

type FlowsAPI struct {
}

func (a FlowsAPI) Name() string {
	return "flows API"
}

func (a FlowsAPI) Bind() {
	tokenAPI := config.C.Router.Group("/v1/app/flows", middleware.TrackLog())
	{
		tokenAPI.GET("report", report)

	}
}

// 第三方流量上报接口
// 本接口主要是阳术国调用
// Auth：hzp
type reportParams struct {
	Uid    int64  `form:"uid" binding:"required"`
	Id     string `form:"id" binding:"required"`
	Type   int    `form:"type" binding:"required"`
	Source int    `form:"source" binding:"required"`
	Sign   string `form:"sign" binding:"required"`
	Mac    string `form:"mac" binding:"required"`
	T      int64  `form:"t" binding:"required"`
}

func report(c *gin.Context) {
	var params reportParams
	err := c.ShouldBindQuery(&params)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	uidStr := strconv.FormatInt(params.Uid, 10)
	typeStr := strconv.Itoa(params.Type)
	sourceStr := strconv.Itoa(params.Source)
	tStr := strconv.FormatInt(params.T, 10)
	err = e.CheckSign(params.Sign, uidStr, params.Id, typeStr, params.Mac, sourceStr, tStr, config.C.ApiKey)
	if err != nil {
		api.SendResponse(c, errno.SignError, nil)
		return
	}

	err = flows.Report(params.Uid, params.T, params.Id, params.Type, params.Source, params.Mac, c.ClientIP())
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}

	errno.OK.Info = "流量上报成功"
	api.SendResponse(c, errno.OK, nil)
	return
}
